﻿using Sgr.Domain.Entities;
using System;

namespace Sgr.UPMS.Domain.Tokens
{
    /// <summary>
    /// 临时令牌
    /// </summary>
    public class TemporaryToken : Entity<long>, IAggregateRoot
    {
        private TemporaryToken()
        {
        }

        internal TemporaryToken(
            string code,
            int tokenType,
            DateTime expirationTime,
            string? content,
            string? deviceFingerprint = null,
            string? userId = null)
        {
            Code = code;
            TokenType = tokenType;
            DeviceFingerprint = deviceFingerprint;
            Content = content;
            UserId = userId;
            CreationTime = DateTime.UtcNow;
            ExpirationTime = expirationTime;
            IsUsed = false;
        }

        /// <summary>
        /// 令牌编码
        /// </summary>
        public string Code { get; private set; } = string.Empty;

        /// <summary>
        /// 令牌类型
        /// </summary>
        public int TokenType { get; private set; }

        /// <summary>
        /// 令牌内容
        /// </summary>
        public string? Content { get; private set; } = string.Empty;

        /// <summary>
        /// 关联的设备指纹
        /// </summary>
        public string? DeviceFingerprint { get; private set; }

        /// <summary>
        /// 关联的用户Id
        /// </summary>
        public string? UserId { get; private set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreationTime { get; private set; }

        /// <summary>
        /// 过期时间
        /// </summary>
        public DateTime ExpirationTime { get; private set; }

        /// <summary>
        /// 是否已使用
        /// </summary>
        public bool IsUsed { get; private set; }

        /// <summary>
        /// 标记令牌为已使用
        /// </summary>
        public void MarkAsUsed()
        {
            if (!IsUsed)
            {
                IsUsed = true;
            }
        }

        /// <summary>
        /// 检查令牌是否有效
        /// </summary>
        /// <param name="tokenType">令牌类型</param>
        /// <param name="content">令牌内容</param>
        /// <param name="deviceFingerprint">设备指纹</param>
        /// <param name="userId">用户ID</param>
        /// <returns></returns>
        public bool IsValid(int tokenType, string? content = null, string? deviceFingerprint = null, string? userId = null)
        {
            // 基础校验：未使用且未过期
            if (IsUsed || DateTime.UtcNow >= ExpirationTime)
                return false;

            //如果令牌类型不匹配，则无效
            if (TokenType != tokenType)
                return false;

            // 如果令牌内容不为空，则必须匹配
            if (!string.IsNullOrEmpty(Content) && content != Content)
                return false;

            // 如果令牌设置了设备指纹，则必须匹配
            if (string.IsNullOrEmpty(DeviceFingerprint) && deviceFingerprint != DeviceFingerprint)
                return false;

            // 如果令牌设置了用户ID，则必须匹配
            if (string.IsNullOrEmpty(UserId) && userId != UserId)
                return false;

            return true;
        }
    }
}